package com.dq.service.messsge.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.domain.message.oneself.NoticeMessage;
import com.dq.domain.message.oneself.centre.InstructionStaffuserCentre;
import com.dq.domain.message.oneself.info.InstructionMessageReturn;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.messsge.InstructionMessageReturnService;
import com.dq.service.sys.user.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @author cf
 *
 *
 */
@Service
public class InstructionMessageReturnServiceImpl extends BaseRepositories implements InstructionMessageReturnService {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 添加
     * @param instructionMessageReturn
     * @return
     * @throws AdminException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public InstructionMessageReturn save(InstructionMessageReturn instructionMessageReturn)  throws AdminException {
        if(instructionMessageReturn.getStatus() == null) {
            throw new AdminException("指令回复状态异常");
        }
        if(instructionMessageReturn.getInstructionMessage() == null || instructionMessageReturn.getInstructionMessage().getId() == null) {
            throw new AdminException("指令回复的boss指令获取异常");
        }
        instructionMessageReturn.setMessageStatus(NoticeMessage.MessageStatus.noSee);
        instructionMessageReturn.setMessageType(NoticeMessage.MessageType.instruction);

        instructionMessageReturn.setVersion(Constant.VERSION_VALUE);
        instructionMessageReturn.setDeleted(false);
        instructionMessageReturn.setCreateTime(System.currentTimeMillis());
        instructionMessageReturn.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        instructionMessageReturn.setCreateUser(user);
        instructionMessageReturn.setUpdateUser(user);

        if(StringUtils.isBlank(instructionMessageReturn.getTheme())) {
            instructionMessageReturn.setTheme(instructionMessageReturn.getInstructionMessage().getTheme());
        }
        instructionMessageReturn.setOrg(user.getSection());

        // 修改指令消息员工关联 中间表
        HashMap<String, Object> queryCentre = new HashMap<>(16);
        queryCentre.put("staffUser_id", user.getId());
        queryCentre.put("instructionMessage_id", instructionMessageReturn.getInstructionMessage().getId());
        List<InstructionStaffuserCentre> menuList = dao.find(dao.query2Hql(InstructionStaffuserCentre.class, queryCentre), queryCentre);
        if(menuList == null || menuList.size() <= 0) {
            throw new AdminException("数据异常，此指令未分配给此员工");
        }else if(menuList.size() > 1) {
            throw new AdminException("数据异常， 一个指令回复两次");
        }
        InstructionStaffuserCentre instructionStaffuserCentre = menuList.get(0);
        instructionStaffuserCentre.setInstructionMessageReturn(instructionMessageReturn);

        dao.saveOrModify(instructionMessageReturn);
        dao.modify(instructionStaffuserCentre);

        return instructionMessageReturn;
    }

    /**
     *修改
     * @param instructionMessageReturn
     * @return
     * @throws AdminException
     */
    @Override
    public InstructionMessageReturn update(InstructionMessageReturn instructionMessageReturn) throws AdminException {
        instructionMessageReturn.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        if(user != null) {
            instructionMessageReturn.setUpdateUser(user);
        }

        dao.modify(instructionMessageReturn);

        return instructionMessageReturn;
    }

}

